package org.budo.warehouse.web.controller;

import java.util.List;

import javax.annotation.Resource;

import org.budo.time.Time;
import org.budo.time.TimePoint;
import org.budo.warehouse.logic.api.ILogPositionLogic;
import org.budo.warehouse.logic.entity.BinlogPosition;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;

/**
 * @author lmw
 */
@Controller
public class LogPositionController {
    private static final String _FORMAT = "yyyy-MM-dd HH:mm:ss.SSS";

    @Resource
    private ILogPositionLogic logPositionLogic;

    @ResponseBody
    @RequestMapping("log_position")
    public String logPosition() {
        List<BinlogPosition> list = logPositionLogic.listLogPosition();

        TimePoint now = Time.now();

        String html = "<table border=\"1\">";

        html += "<tr>";
        html += "<td colspan=\"" + 3 + "\"></td>";
        html += "<td colspan=\"" + 4 + "\"></td>";
        html += "<td colspan=\"" + 3 + "\">" + now.toString(_FORMAT) + "</td>";
        html += "</tr>";
        html += "<tr>";

        html += "<td>DESTINATION</td>";
        html += "<td>MASTER</td>";
        html += "<td>POSITION</td>";
        html += "<td>SERVER_ID</td>";
        html += "<td>SLAVE_ID</td>";
        html += "<td>JOURNAL</td>";
        html += "<td>POSITION</td>";
        html += "<td>TIMESTAMP</td>";
        html += "<td>DIFF POSITION</td>";
        html += "<td>DIFF TIME</td>";
        html += "</tr>";

        for (BinlogPosition position : list) {
            Long masterPosition = position.getMasterPosition();
            Long slavePosition = position.getSlavePosition();

            html += "<tr>";
            html += "<td>" + position.getDestination() + "</td>";
            html += "<td>" + position.getMasterFile() + "</td>";
            html += "<td>" + masterPosition + "</td>";
            html += "<td>" + position.getServerId() + "</td>";
            html += "<td>" + position.getSlaveId() + "</td>";

            html += "<td>" + position.getSlaveJournalName() + "</td>";
            html += "<td>" + slavePosition + "</td>";

            TimePoint slaveTime = Time.when(position.getSlaveTimestamp());
            html += "<td>" + slaveTime.toString(_FORMAT) + "</td>";

            html += "<td>" + (null != masterPosition && null != slavePosition ? (masterPosition - slavePosition) : "") + "</td>";
            html += "<td>" + (null != masterPosition && null != slavePosition ? now.between(slaveTime).toMilliSeconds() : "") + "</td>";

            html += "</tr>";
        }

        html += "</table>";
        return html;
    }

    @ResponseBody
    @RequestMapping("log_position.json")
    public String logPositionJson() {
        List<BinlogPosition> list = logPositionLogic.listLogPosition();

        return JSON.toJSONString(list);
    }
}